package com.dlyk.manager;

import com.dlyk.mapper.TActivityMapper;
import com.dlyk.mapper.TClueMapper;
import com.dlyk.mapper.TCustomerMapper;
import com.dlyk.mapper.TTranMapper;
import com.dlyk.result.NameValue;
import com.dlyk.result.SummaryData;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class StatisticManager {

    @Autowired
    private TActivityMapper activityMapper;

    @Autowired
    private TClueMapper ClueMapper;

    @Autowired
    private TTranMapper TranMapper;

    @Autowired
    private TCustomerMapper customerMapper;


    /**
     * 概要数据统计
     * @return
     */
    public SummaryData summaryData(){

        // 有效市场活动总数
        Integer effectiveActivityCount=activityMapper.getOnActivity().size();

        // 总的市场活动数
        Integer totalActivityCount=activityMapper.selectActivityCount();

        // 线索总数
        Integer totalClueCount=ClueMapper.selectClueCount();

        // 客户总数
        Integer totalCustomerCount=customerMapper.selectCustomerCount();

        // 成功的交易额
        BigDecimal successTranAmount=TranMapper.selectSuccessTranAmount();

        // 总交易额
        BigDecimal totalTranAmount=TranMapper.selectTranAmountCount();

        return SummaryData
                .builder()
                .effectiveActivityCount(effectiveActivityCount)
                .totalActivityCount(totalActivityCount)
                .totalClueCount(totalClueCount)
                .totalCustomerCount(totalCustomerCount)
                .successTranAmount(successTranAmount)
                .totalTranAmount(totalTranAmount)
                .build();
    }


    /**
     * 销售统计数据
     * @return
     */
    public List<NameValue> loadSaleFunnel() {

        List<NameValue> nameValueList=new ArrayList<>();

        // 客户总数
        Integer customerCount = customerMapper.selectCustomerCount();

        // 线索总数
        Integer clueCount = ClueMapper.selectClueCount();

        // 交易总数
        int tranCount=TranMapper.selectByTotalTranCount();

        // 成交量
        int successTotalTranCount=TranMapper.selectBySuccessTotalTranCount();

        NameValue successTotalTran=NameValue.builder().name("成交").value(successTotalTranCount).build();
        nameValueList.add(successTotalTran);

        NameValue tranCounts=NameValue.builder().name("交易").value(tranCount).build();
        nameValueList.add(tranCounts);

        NameValue clue=NameValue.builder().name("线索").value(clueCount).build();
        nameValueList.add(clue);

        NameValue customer=NameValue.builder().name("客户").value(customerCount).build();
        nameValueList.add(customer);

        return nameValueList;
    }

    /**
     * 线索来源统计
     * @return
     */
    public List<NameValue> loadResourcePie() {
        return ClueMapper.selectByResource();
    }
}
